package com.gjj.abox.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.gjj.abox.BaseActivity;
import com.gjj.abox.database.BoxDB;
import com.gjj.abox.database.R;
import com.gjj.abox.utils.AnimalUtils;
import com.gjj.abox.utils.ToastUtil;


public class RegisterSecondActivity extends BaseActivity implements View.OnClickListener{

	private Spinner spinner;
	private EditText etAnswer;
	private Button btnSum;
	private ImageView ivBack;
	private String name, pwd, ques;
	private ArrayAdapter<String> adapter;
	private final String PREFERENCE_NAME = "source";
	private SharedPreferences sharedpreferences = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_second);
		sharedpreferences = getSharedPreferences(PREFERENCE_NAME,
				Activity.MODE_PRIVATE);
		name = sharedpreferences.getString("name", "");
		pwd = sharedpreferences.getString("pwd", "");
		etAnswer = (EditText) findViewById(R.id.et_set_question);
		spinner = (Spinner) findViewById(R.id.spinner_set_question);
		btnSum = (Button) findViewById(R.id.btn_question);
		ivBack = (ImageView) findViewById(R.id.iv_register_question_back);
		ivBack.setOnClickListener(this);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.account_question_list));
		adapter.setDropDownViewResource(R.layout.spinner_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				ques = getResources().getStringArray(R.array.account_question_list)[arg2];
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}

		});
		btnSum.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.iv_register_question_back:
				finish();
				overridePendingTransition(0,R.anim.slide_right_out);
				break;
			case R.id.btn_question:
				if (etAnswer.getText().toString().equals("")) {
					AnimalUtils.shakeAnimal(etAnswer);
				} else {
					BoxDB box = new BoxDB(getApplicationContext());
					box.insertRegister(name, pwd, ques, etAnswer.getText()
							.toString());
					ToastUtil.showShortToast(RegisterSecondActivity.this,"注册成功！");
					Intent intent = new Intent(RegisterSecondActivity.this, LoginActivity.class);
					startActivity(intent);
					finish();
				}
				break;
		}
	}
}
